$(document).ready(function () {
    var userstring = sessionStorage.getItem('user');
    var user = JSON.parse(userstring);
    var nickname=user.nickname;
    $('#nickname').text(nickname);
    var userid=user.id;

    var pageindex = 1;
    $.get("orderitem/showOrderItemsList",{"userid":userid,"pageindex":pageindex},function (js) {
        show(js);
    });

    $("[type = 'button']").click(function () {
        var pageindex = $(this).val();
        $.get("orderitem/showOrderItemsList",{"userid":userid,"pageindex":pageindex},function (js) {
            show(js);
        });
    });

});

function show(js) {
    JSON.stringify(js);
    var imageMap = {
        "进口柠檬": "images/goods/goods001.jpg",
        "玫瑰香葡萄": "images/goods/goods002.jpg",
        "大兴大棚草莓": "images/goods/goods003.jpg",
        "吐鲁番梨光杏": "images/goods/goods004.jpg",
        "黄肉桃": "images/goods/goods005.jpg",
        "进口西梅": "images/goods/goods006.jpg",
        "香梨": "images/goods/goods007.jpg",
        "栗子": "images/goods/goods008.jpg",
        "海南香蕉": "images/goods/goods009.jpg",
        "青苹果": "images/goods/goods010.jpg",
        "山莓": "images/goods/goods011.jpg",
        "奇异果": "images/goods/goods012.jpg",
        "蜜桔": "images/goods/goods013.jpg",
        "脐橙": "images/goods/goods014.jpg",
        "糯米椰子": "images/goods/goods015.jpg",
        "碧绿红心李子": "images/goods/goods016.jpg",
        "红樱桃": "images/goods/goods017.jpg",
        "新鲜生虾": "images/goods/goods018.jpg",
        "扇贝": "images/goods/goods019.jpg",
        "秋刀鱼": "images/goods/goods020.jpg",
        "斑节虾": "images/goods/goods021.jpg",
        "猪五花肉": "images/goods/goods022.jpg",
        "牛肩肉": "images/goods/goods023.jpg",
        "新鲜羊肉": "images/goods/goods024.jpg",
        "澳洲牛排": "images/goods/goods025.jpg",
        "鸡蛋": "images/goods/goods026.jpg",
        "鸡胸肉": "images/goods/goods027.jpg",
        "咸鸭蛋": "images/goods/goods028.jpg",
        "速冻鸡腿肉": "images/goods/goods029.jpg",
        "正宗东北大白菜": "images/goods/goods030.jpg",
        "芹菜": "images/goods/goods031.jpg",
        "好吃的香菜": "images/goods/goods032.jpg",
        "粉皮冬瓜": "images/goods/goods033.jpg",
        "速冻鱼丸": "images/goods/goods034.jpg",
        "蟹柳": "images/goods/goods035.jpg",
        "手拍虾丸": "images/goods/goods036.jpg",
        "玉米猪肉蒸饺": "images/goods/goods037.jpg"
    };

    $("form").empty();
    $("ul[id = \"978\"]").empty();
    $("ul[id = \"979\"]").empty();
        for(var i=0;i<js.result.arrayList.pageSize;i++){
            if ( js.result.arrayList.list[i]==null){
                break;
            }
            $("<li class=\"col01\">" + js.result.arrayList.list[i].createDate + "</li>\n" +
                "            <li name='"+(i+2)+"' class=\"col02\" value='" + js.result.arrayList.list[i].orderNumber + "'>" + '订单号：' + js.result.arrayList.list[i].orderNumber + "</li>\n" +
                "            <li class=\"col02 stress\">" + js.result.arrayList.list[i].status + "</li>").appendTo("ul[id = "+(978+i)+"]");
            $(
                "<table class='order_list_table w980'>" +
                "<tbody>" +
                "<tr>" +
                "<td width='55%'>"+
                xunhuan(js,i,imageMap)+
                "</td>"+
                "<td width='15%'>" + total(js,i) + "元</td>" +
                "<td width='15%'>" + js.result.arrayList.list[i].status + "</td>" +
                "<td width='15%'>" + select(js.result.arrayList.list[i].status,i,js) + "</td>" +
                "</tr>" +
                "</tbody>" +
                "</table>").appendTo("[id=\"form"+(i+1)+"\"]");

        }




/*    $("<li class=\"col01\">" + js.result.arrayList.list[0].createDate + "</li>\n" +
        "            <li name='01' class=\"col02\" value='" + js.result.arrayList.list[0].orderNumber + "'>" + '订单号：' + js.result.arrayList.list[0].orderNumber + "</li>\n" +
        "            <li class=\"col02 stress\">" + js.result.arrayList.list[0].status + "</li>").appendTo("ul[class = \"order_list_th w978 clearfix\"]");
    $(
        "<table class='order_list_table w980'>" +
        "<tbody>" +
        "<tr>" +
        "<td width='55%'>"+
        xunhuan0(js,imageMap)+
        "</td>"+
        "<td width='15%'>" + total(js,0) + "元</td>" +
        "<td width='15%'>" + js.result.arrayList.list[0].status + "</td>" +
        "<td width='15%'>" + select0(js.result.arrayList.list[0].status) + "</td>" +
        "</tr>" +
        "</tbody>" +
        "</table>").appendTo("[id=\"formA\"]");*/


/*
    $("<li class=\"col01\">" + js.result.arrayList.list[1].createDate + "</li>\n" +
        "            <li name='10' class=\"col02\" value='" + js.result.arrayList.list[1].orderNumber + "'>" + '订单号：' + js.result.arrayList.list[1].orderNumber + "</li>\n" +
        "            <li class=\"col02 stress\">" + js.result.arrayList.list[1].status + "</li>").appendTo("ul[class = \"order_list_th w979 clearfix\"]");
    $(
        "<table class='order_list_table w980'>" +
        "<tbody>" +
        "<tr>" +
        "<td width='55%'>"+
        xunhuan1(js,imageMap)+
        "</td>"+
        "<td width='15%'>" + total(js,1) + "元</td>" +
        "<td width='15%'>" + js.result.arrayList.list[1].status + "</td>" +
        "<td width='15%'>" + select1(js.result.arrayList.list[1].status) + "</td>" +
        "</tr>" +
        "</tbody>" +
        "</table>").appendTo("[id=\"formB\"]");*/


    $("[class = 'pagenation']").empty();
    $("<input type=\"button\" value=\"<\" />\n" +
        page(js) +
        "            <input type=\"button\" value=\">\" />").appendTo("[class = 'pagenation']");
};

$(document).on("click","[name = '0']",function () {
    var ordernumber = $("[name = '2']").val();

    $.get("orderitem/payOrderItems",{"ordernumber":ordernumber},function (rs) {
        if(rs.result){
            alert("付款成功！");
            location.reload();
        }else {
            alert("付款失败！");
            location.reload();
        }
    });
});

$(document).on("click","[name = '1']",function () {
    var ordernumber = $("[name = '3']").val();
    $.get("orderitem/payOrderItems",{"ordernumber":ordernumber},function (rs) {
        if(rs.result){
            alert("付款成功！");
            location.reload();
        }else {
            alert("付款失败！");
            location.reload();
        }
    });
});

$(document).on("click","[type = 'button']",function () {
    var userstring = sessionStorage.getItem('user');
    var user = JSON.parse(userstring);
    var nickname=user.nickname;
    var userid=user.id;
    var pageindex = $(this).val();
    $.get("orderitem/showOrderItemsList",{"userid":userid,"pageindex":pageindex},function (js) {
        show(js);
    });
});

function select(status,s,js) {
    if(status==="已支付"){
        return "";
    }else {
        return "<input name='"+s+"' type='button' value='去付款'/>";

    }

};


function xunhuan(js,s,imageMap) {
    var str="";
    for (var n = 0; n < js.result.arrayLists.length; n++) {
        if (js.result.arrayList.list[s].orderNumber === js.result.arrayLists[n].orderNumber) {
            if (js.result.arrayLists[n].status === js.result.arrayList.list[s].status) {
                str = str +
                    "<ul class='order_goods_list clearfix'>" +
                    "<li class='col01'><img src='" + (imageMap[js.result.arrayLists[n].fname] || imageMap) + "'></li>" +
                    "<li class='col02'>" + js.result.arrayLists[n].fname + '<em>' + js.result.arrayLists[n].price + '元/500g</em>' + "</li>" +
                    "<li class='col03'>" + js.result.arrayLists[n].quantity + "</li>" +
                    "<li class='col04'>" + js.result.arrayLists[n].price*js.result.arrayLists[n].quantity + '元' + "</li>" +
                    "</ul>";
            }
        }
    }
    return str;
};

function page(js) {
    var str = "";
    for(var n = 0;n<js.result.arrayList.pages;n++){
        str = str + "<input type ='button'  value='"+(n+1)+"' />"
    }

    return str;
}

function total(js,s) {
    var totalprice=0;
    for (var n = 0; n < js.result.arrayLists.length; n++) {
        if (js.result.arrayList.list[s].orderNumber === js.result.arrayLists[n].orderNumber) {
            if (js.result.arrayLists[n].status === js.result.arrayList.list[s].status) {
                totalprice = totalprice + js.result.arrayLists[n].price*js.result.arrayLists[n].quantity;
            }
        }
    }
    return totalprice;
}
